package rentcar.web;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;



import rentcar.domain.Car;
import rentcar.domain.Request;
import rentcar.domain.UserPrincipal;
import rentcar.domain.requestStatus;
import rentcar.service.CarService;
import rentcar.service.RequestService;
import rentcar.service.UserService;

@Controller
@RequestMapping("/request")
public class RequestController {
	
	@Autowired
	private RequestService requestService;
	
	@Autowired
	private CarService carService;	

	@Autowired
	private UserService userService;	

	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listRequests() {

		ModelAndView mav = new ModelAndView();
		mav.addObject("requests", requestService.getRequests());
		mav.setViewName("request.list");
		return mav;
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String showRequestProfile(
			@PathVariable("id") Request request,
			HttpSession session, Model model) {

		model.addAttribute("request", request);
		
		return "request.details";
	}	
	

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editRequestForm(
			@PathVariable("id") Request request,
			HttpSession session, Model model) {

		populateControls(model);		
		
	//	request.setRentedCar(   request.getRentedCar()  );
	//	request.setClientTr( Long.toString(request.getClient().getUserId())  );
	//	request.setManagerTr( Long.toString(request.getManager().getUserId()) );
		
		model.addAttribute("request", request);
		return "request.form.edit";
	}	

	
	private void populateControls(Model model) {
		
	//-------------Combo Box of Cars ------------------------------------	
        List<Car> inCarTable;
		inCarTable = carService.getCars();
        	
		Map<String, String> carList = new LinkedHashMap<String, String>();
		
		//for (int i = 0; i <  inCarTable.size(); i++) {
		//	carList.put( Long.toString(inCarTable.get(i).getcarId() ), inCarTable.get(i).getCarBrand() + " | " + inCarTable.get(i).getColor() );
		//	}
		
		for (Car c: inCarTable) {
				carList.put( Long.toString(c.getcarId() ), c.getCarBrand()+ " | " + c.getColor() + " | " + c.getStateNumber() );
		}

   		model.addAttribute("carList", carList );
		
   		//model.addAttribute("carList", inCarTable );

   		//------------------Combo Box of Clients and Managers -----------------------------------
        List<UserPrincipal> inClientTable;
		inClientTable = userService.getUsers();
   		
		Map<String, String> clientList = new LinkedHashMap<String, String>();
		for (int i = 0; i <  inClientTable.size(); i++) {
			clientList.put( Long.toString(inClientTable.get(i).getUserId() ), inClientTable.get(i).getName());
		}
		model.addAttribute("clientList", clientList );
   		
		model.addAttribute("managerList", clientList );    
		
		//-----------------Combo Вох for  Status -------------------------------------------------
		
		Map<String, String> statusList = new LinkedHashMap<String, String>();
		
		for (requestStatus rs : requestStatus.values())	{
		   //accessList.put( Integer.toString(ar.ordinal()), ar.toString());
		   statusList.put( Integer.toString(rs.ordinal()), rs.toString());
		   System.out.println("-------------------|" + Integer.toString(rs.ordinal()) +"|--------------|"+rs.toString() );
		   
		}
		model.addAttribute("statusList", statusList);

		
	}
	

	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public String editRequestSubmit(
			@ModelAttribute Request request, 
			Model model) {
		
		if (request != null) {
			//System.out.println("active:" + client.isActive());
		
/*			long car_n;
			car_n = 6;
			request.setRentedCar( carService.getCarByID(car_n) );

			long user_n = 2;
			long manager_n = 5;
            request.setClient( userService.getUserByID(user_n) );
			request.setManager( userService.getUserByID(manager_n) );*/
			
			System.out.println(request.toString());
			requestService.updateRequest(request);	
			
			model.addAttribute("requests", requestService.getRequests());
		}
		
		model.asMap().remove("request");
		return "request.list";
	}	
	
	
}
